package controllers

import (
	"github.com/astaxie/beego"
	"go-cms/consts"
	"go-cms/models"
	"strings"
)

type BaseController struct {
	beego.Controller
	controllerName string
	actionName     string
}

// 准备
func (c *BaseController) Prepare() {
	// 赋值
	c.controllerName, c.actionName = c.GetControllerAndAction()
	beego.Informational(c.controllerName, c.actionName)

	//user := c.auth()
	c.Data["Menu"] = models.MenuStruct()
}

// 设置模板
// 第一个参数模板，第二参数为layout
func (c *BaseController) setTpl(template ...string) {
	var tplName string
	layout := "common/layout.html"
	switch {
	case len(template) == 1:
		tplName = template[0]
	case len(template) == 2:
		tplName = template[0]
		layout = template[1]
	default:
		// 不要 "Controller"这10个字母
		ctrlName := strings.ToLower(c.controllerName[0 : len(c.controllerName)-10])
		actionName := strings.ToLower(c.actionName)
		tplName = ctrlName + "/" + actionName + ".html"
	}

	_,found := c.Data["Footer"]
	if !found{
		c.Data["Footer"] = "menu/footerjs.html"
	}
	c.Layout = layout
	c.TplName = tplName

	// LOGO地址
	c.Data["LogoSrc"]="./static/img/logo.png"

	// 菜单
	m := models.MenuStruct()
	c.Data["Menu"] = m
}
func (c *BaseController) jsonResult(code consts.JsonResultCode, msg string, obj interface{}) {
	r := &models.JsonResult{code, msg, obj}
	c.Data["json"] = r
	c.ServeJSON()
	c.StopRun()
}

func (c *BaseController)listJsonResult(code consts.JsonResultCode,msg string,count int64,obj interface{}){
	r := &models.ListJsonResult{code,msg,count,obj}
	c.Data["json"] = r
	c.ServeJSON()
	c.StopRun()
}
